/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.communication.util;

import jakarta.annotation.Resource;
import org.gokeep.common.component.TokenComponent;
import org.gokeep.common.config.GlobalConfig;
import org.gokeep.communication.proto.CommandProto;
import org.gokeep.communication.proto.MessageProto;
import org.springframework.stereotype.Component;

@Component
public class MessageVerify {

    @Resource
    private TokenComponent tokenComponent;

    public boolean verify(MessageProto.Message message) {
        // 先检查消息头信息
        if (message.getProtocol() != GlobalConfig.MESSAGE_PROTOCOL_HEAD) {
            return false;
        }
        // 如果是心跳消息，则不进行校验
        if (message.getCommand().equals(CommandProto.CommandEnum.HEART)) {
            return true;
        }
        String token = message.getToken();
        return tokenComponent.verify(token);
    }
}